package com.orangelife.service.propFeeComputeRule.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.PropFeeType.model.PropFeeTypeDto;
import com.orangelife.service.feeDetail.IFeeDetailService;
import com.orangelife.service.feeDetail.modal.PropFeeDetailDto;
import com.orangelife.service.paybill.IPayBillService;
import com.orangelife.service.propFeeCommInfo.IPropFeeCommInfoService;
import com.orangelife.service.propFeeComputeRule.IPropFeeComputeRuleService;
import com.orangelife.service.propFeeComputeRule.model.PropFeeComputeRuleDto;
import com.orangelife.service.propFeeComputeRule.model.Ratio;
import com.orangelife.service.propFeeComputeRule.model.Rule;
import com.orangelife.service.propFeeRuleLinkApartment.IPropFeeRuleLinkApartmentService;
import com.orangelife.service.propFeeRuleLinkApartment.model.PropFeeRuleLinkApartmentDto;
import com.orangelife.service.propFeeRuleLinkMeter.model.PropFeeRuleLinkMeterDto;
import com.orangelife.service.propFeeRuleParam.model.PropFeeRuleParamDto;
import com.orangelife.service.propinfo.IPropInfoService;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.propFeeComputeRule.entity.PropFeeComputeRule;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeComputeRule类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 10:41:10
 * ==============================================================
 */
@Service("propFeeComputeRuleService")
public class PropFeeComputeRuleServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeComputeRuleService {

	@Resource(name = "feeDetailService")
	private IFeeDetailService feeDetailService;
	@Resource(name = "propInfoService")
	private IPropInfoService propInfoService;

	@Resource(name = "propFeeTypeService")
	private IPropFeeTypeService propFeeTypeService;

	@Resource(name = "propFeeRuleLinkApartmentService")
	private IPropFeeRuleLinkApartmentService propFeeRuleLinkApartmentService;

	@Resource(name = "payBillService")
	private IPayBillService payBillService;

	@Resource(name = "propFeeCommInfoService")
	private IPropFeeCommInfoService propFeeCommInfoService;

	private static Logger logger = Logger
			.getLogger(PropFeeComputeRuleServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeComputeRuleServiceImpl.logger = logger;
	}

	private static Map<String, String> ruleCheckParam = new HashMap<String, String>();
	static {

		ruleCheckParam.put("@001", "1");
		ruleCheckParam.put("@002", "1");
		ruleCheckParam.put("@003", "1");
		ruleCheckParam.put("@004", "1");
		ruleCheckParam.put("@005", "2");
		ruleCheckParam.put("@006", "2");
		ruleCheckParam.put("@008", "3,4");
		ruleCheckParam.put("@009", "4");
		ruleCheckParam.put("@010", "5");
		ruleCheckParam.put("@011", "6");
		ruleCheckParam.put("@012", "6");
		ruleCheckParam.put("@013", "7");
		ruleCheckParam.put("@014", "4");
		ruleCheckParam.put("@015", "8");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeComputeRule(PropFeeComputeRuleDto propFeeComputeRule)
			throws AppBizException {

		Assert.notNull(propFeeComputeRule);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			if (propFeeComputeRule.getRuleName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			if (propFeeComputeRule.getRuleType().length() > 20) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRule())) {
			if (propFeeComputeRule.getRule().length() > 3000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRoundType())) {
			if (propFeeComputeRule.getRoundType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"进位类型 1四舍五入（默认）2舍去长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			if (propFeeComputeRule.getPeriodType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"生成周期(1按月，2按季)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getCurDate())) {
			if (propFeeComputeRule.getCurDate().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "当前生成日期长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			if (propFeeComputeRule.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			propFeeComputeRule.setRuleID(IdUtil.getuuId());
		}
	
		propFeeComputeRule.setItemStat(TypeConst.FLAG_ON);
		propFeeComputeRule.setTscreate(new Date());
		propFeeComputeRule.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeComputeRuleMapper.insertSelective", propFeeComputeRule);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeComputeRule(PropFeeComputeRuleDto propFeeComputeRule)
			throws AppBizException {

		Assert.notNull(propFeeComputeRule);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeComputeRuleDto oldPropFeeComputeRule = this
				.findPropFeeComputeRuleByID(propFeeComputeRule.getRuleID());

		if (!StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			if (propFeeComputeRule.getRuleName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			if (propFeeComputeRule.getRuleType().length() > 20) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRule())) {
			if (propFeeComputeRule.getRule().length() > 3000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getRoundType())) {
			if (propFeeComputeRule.getRoundType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"进位类型 1四舍五入（默认）2舍去长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			if (propFeeComputeRule.getPeriodType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"生成周期(1按月，2按季)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getCurDate())) {
			if (propFeeComputeRule.getCurDate().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "当前生成日期长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			if (propFeeComputeRule.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (oldPropFeeComputeRule == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeComputeRule.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeComputeRuleMapper.updateByPrimaryKeySelective",
				propFeeComputeRule);
	}

	@Override
	public PropFeeComputeRuleDto findPropFeeComputeRuleByID(
			String propFeeComputeRuleID) throws AppBizException {
		if (StringUtils.isBlank(propFeeComputeRuleID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeComputeRuleDto oldPropFeeComputeRule = this.getSqlSession()
				.selectOne("PropFeeComputeRuleSearchMapper.selectByPrimaryKey",
						propFeeComputeRuleID);
		if (oldPropFeeComputeRule == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeComputeRule.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeComputeRule;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeComputeRuleByLogic(String propFeeComputeRuleID,
			String edituserID) throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRuleID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeComputeRuleDto oldPropFeeComputeRule = this
				.findPropFeeComputeRuleByID(propFeeComputeRuleID);

		List<Map<String, Object>> feeDetails = feeDetailService
				.checkRuleIDIsUsed(null, null, null, null, null,
						oldPropFeeComputeRule.getRuleID(), null, null);
		if (feeDetails != null && feeDetails.size() > 0) {
			throw new AppBizException(2, "亲，该规则已经被使用");
		}

		/***
		 * 删除关联连接
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", propFeeComputeRuleID);
		map.put("edituserID", edituserID);

		this.getSqlSession().update(
				"PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleID", map);

		this.getSqlSession().update(
				"PropFeeRuleLinkMeterBauthMapper.bauthDeleteByRuleID", map);

		this.getSqlSession().update(
				"PropFeeRuleParamBauthMapper.bauthDeleteByRuleID", map);
		oldPropFeeComputeRule.setEdituserID(edituserID);
		oldPropFeeComputeRule.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeComputeRule.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeComputeRuleMapper.updateByPrimaryKeySelective",
				oldPropFeeComputeRule);
	}

	@Override
	public List<Map<String, Object>> findPropFeeComputeRuleByPage(String type,
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("commID", commID);
		map.put("type", type);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleSearchMapper.findAllPropFeeComputeRule",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> getPropFeeComputeRuleByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeComputeRuleDto> list = this.getSqlSession().selectList(
				"PropFeeComputeRuleSearchMapper.getAllPropFeeComputeRuleDto",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeComputeRuleByRelevantID(
			String ruleName, String ruleID, String commID, String feeTypeID,
			String ruleType, String roundType, String periodType, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleName", ruleName);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("feeTypeID", feeTypeID);
		map.put("ruleType", ruleType);
		map.put("roundType", roundType);
		map.put("periodType", periodType);
		map.put("status", status);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleSearchMapper.selectPropFeeComputeRuleDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> findPropFeeComputeRuleByRelevantID(
			String ruleName, String ruleID, String commID, String feeTypeID,
			String ruleType, String roundType, String periodType, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleName", ruleName);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("feeTypeID", feeTypeID);
		map.put("ruleType", ruleType);
		map.put("roundType", roundType);
		map.put("periodType", periodType);
		map.put("status", status);

		List<PropFeeComputeRuleDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleSearchMapper.findPropFeeComputeRuleDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> bauthfindPropFeeComputeRule(
			List<String> propFeeComputeRuleIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeComputeRuleDto> list = this.getSqlSession().selectList(
				"PropFeeComputeRuleSearchMapper.batchFindPropFeeComputeRule",
				propFeeComputeRuleIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeComputeRuleByMap(
			Map<String, Object> propFeeComputeRule) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleSearchMapper.batchFindPropFeeComputeRuleByMap",
						propFeeComputeRule);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}
		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {

			// bauthfindPropFeeCommInfoByMap

			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}

		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}
		/*
		 * List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
		 * .findPropFeeComputeRuleByRelevantID(
		 * propFeeComputeRule.getRuleName(), null,
		 * propFeeComputeRule.getCommID(), propFeeComputeRule.getFeeTypeID(),
		 * propFeeComputeRule.getRuleType(), propFeeComputeRule.getRoundType(),
		 * propFeeComputeRule.getPeriodType(), propFeeComputeRule.getStatus());
		 */
		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);
		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 0) {
			throw new AppBizException(2, "亲,该类目下该名称规则已经存在");

		}
		propFeeComputeRule.setRuleID(IdUtil.getuuId());
		propFeeComputeRule.setInitID(propFeeComputeRule.getRuleID());
		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());

		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {

			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (1==1||!propFeeTypeDto.getType().equals("3")
					|| (propFeeTypeDto.getType().equals("3") && propFeeComputeRule
							.getValidityType().equals("2"))) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());

				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {
						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {

				/***
				 * 0固定, 1公摊类，2计量类，3临时类'
				 */

				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
			}

			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}

		/***
		 * 添加参数
		 */
		return this.addPropFeeComputeRule(propFeeComputeRule);

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			throw new AppBizException(2, "亲，规则信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}
		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			// propFeeComputeRule.setPeriodType("1");
			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}

		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}
		/*
		 * List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
		 * .findPropFeeComputeRuleByRelevantID(
		 * propFeeComputeRule.getRuleName(), null,
		 * propFeeComputeRule.getCommID(), propFeeComputeRule.getFeeTypeID(),
		 * propFeeComputeRule.getRuleType(), propFeeComputeRule.getRoundType(),
		 * propFeeComputeRule.getPeriodType(), propFeeComputeRule.getStatus());
		 */

		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);
		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 1) {
			throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");

		}
		if (propFeeComputeRuleDtos != null
				&& propFeeComputeRuleDtos.size() == 1) {
			if (!propFeeComputeRuleDtos.get(0).getRuleID()
					.equals(propFeeComputeRule.getRuleID())) {
				throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
			}
		}
		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());

		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {
			/***
			 * 删除关联连接
			 */
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ruleID", propFeeComputeRule.getRuleID());
			map.put("edituserID", propFeeComputeRule.getEdituserID());
			this.getSqlSession().update(
					"PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleID",
					map);

			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (1==1||!propFeeTypeDto.getType().equals("3")
					|| (propFeeTypeDto.getType().equals("3") && propFeeComputeRule
							.getValidityType().equals("2"))) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());

				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {

						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */

			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {

				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getEdituserID());
			}
			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		/***
		 * 添加参数
		 */
		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			/***
			 * 删除关联连接
			 */
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ruleID", propFeeComputeRule.getRuleID());
			map.put("edituserID", propFeeComputeRule.getEdituserID());
			this.getSqlSession().update(
					"PropFeeRuleParamBauthMapper.bauthDeleteByRuleID", map);
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getEdituserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}

		/*
		 * String periodVal = propFeeCommInfoService.findValByCode(
		 * propFeeComputeRule.getCommID(), "period"); if
		 * (StringUtils.isBlank(periodVal)) { throw new AppBizException(2,
		 * "亲，请先小区配置缴费周期"); }
		 * 
		 * propFeeComputeRule.setPeriodType(periodVal);
		 */
		/***
		 * 添加参数
		 */
		return this.editPropFeeComputeRule(propFeeComputeRule);

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPoolTypeFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}
		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}

		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}
		/*
		 * List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
		 * .findPropFeeComputeRuleByRelevantID(
		 * propFeeComputeRule.getRuleName(), null,
		 * propFeeComputeRule.getCommID(), propFeeComputeRule.getFeeTypeID(),
		 * propFeeComputeRule.getRuleType(), propFeeComputeRule.getRoundType(),
		 * propFeeComputeRule.getPeriodType(), propFeeComputeRule.getStatus());
		 */

		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);

		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 0) {
			throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
		}
		propFeeComputeRule.setRuleID(IdUtil.getuuId());
		propFeeComputeRule.setInitID(propFeeComputeRule.getRuleID());
		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {

			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (!propFeeTypeDto.getType().equals("3")) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());

				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {

						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {

				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
			}

			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		/****
		 * 添加公摊关联
		 */
		if (propFeeComputeRule.getPropFeeRuleLinkMeter() != null
				&& propFeeComputeRule.getPropFeeRuleLinkMeter().size() > 0) {
			for (PropFeeRuleLinkMeterDto propFeeRuleLinkMeter : propFeeComputeRule
					.getPropFeeRuleLinkMeter()) {
				propFeeRuleLinkMeter.setCommID(propFeeComputeRule.getCommID());
				propFeeRuleLinkMeter.setRuleID(propFeeComputeRule.getRuleID());
				propFeeRuleLinkMeter.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleLinkMeter.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkMeter.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkMeter.setTscreate(new Date());
				propFeeRuleLinkMeter.setTsedit(new Date());
			}
			this.getSqlSession().insert(
					"PropFeeRuleLinkMeterBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkMeter());
		}

		/***
		 * 添加参数
		 */
		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}
		/***
		 * 添加参数
		 */
		return this.addPropFeeComputeRule(propFeeComputeRule);

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPoolTypeFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}

		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}

		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}

		/***
		 * 判断房屋关联
		 */
		/*
		 * List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
		 * .findPropFeeComputeRuleByRelevantID(
		 * propFeeComputeRule.getRuleName(), null,
		 * propFeeComputeRule.getCommID(), propFeeComputeRule.getFeeTypeID(),
		 * propFeeComputeRule.getRuleType(), propFeeComputeRule.getRoundType(),
		 * propFeeComputeRule.getPeriodType(), propFeeComputeRule.getStatus());
		 */

		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);

		// List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = this
		// .findPropFeeComputeRuleByRelevantID(null, null,
		// propFeeComputeRule.getCommID(),
		// propFeeComputeRule.getFeeTypeID(), null, null, null,
		// null);

		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 1) {
			throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");

		}
		if (propFeeComputeRuleDtos != null
				&& propFeeComputeRuleDtos.size() == 1) {
			if (!propFeeComputeRuleDtos.get(0).getRuleID()
					.equals(propFeeComputeRule.getRuleID())) {
				throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
			}
		}

		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());

		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {
			/***
			 * 删除关联连接
			 */
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ruleID", propFeeComputeRule.getRuleID());
			map.put("edituserID", propFeeComputeRule.getEdituserID());
			this.getSqlSession().update(
					"PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleID",
					map);

			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (1==1||!propFeeTypeDto.getType().equals("3")
					|| (propFeeTypeDto.getType().equals("3") && propFeeComputeRule
							.getValidityType().equals("2"))) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());
				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {

						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {

							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getEdituserID());
			}
			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		/****
		 * 添加公摊关联
		 */
		if (propFeeComputeRule.getPropFeeRuleLinkMeter() != null
				&& propFeeComputeRule.getPropFeeRuleLinkMeter().size() > 0) {

			/***
			 * 删除关联连接
			 */
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ruleID", propFeeComputeRule.getRuleID());
			map.put("edituserID", propFeeComputeRule.getEdituserID());
			this.getSqlSession().update(
					"PropFeeRuleLinkMeterBauthMapper.bauthDeleteByRuleID", map);
			for (PropFeeRuleLinkMeterDto propFeeRuleLinkMeter : propFeeComputeRule
					.getPropFeeRuleLinkMeter()) {
				propFeeRuleLinkMeter.setCommID(propFeeComputeRule.getCommID());
				propFeeRuleLinkMeter.setRuleID(propFeeComputeRule.getRuleID());
				propFeeRuleLinkMeter.setCreateuserID(propFeeComputeRule
						.getEdituserID());
				propFeeRuleLinkMeter.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkMeter.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkMeter.setTscreate(new Date());
				propFeeRuleLinkMeter.setTsedit(new Date());
			}
			this.getSqlSession().insert(
					"PropFeeRuleLinkMeterBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkMeter());
		}

		/***
		 * 添加参数
		 */
		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			/***
			 * 删除关联连接
			 */
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ruleID", propFeeComputeRule.getRuleID());
			map.put("edituserID", propFeeComputeRule.getEdituserID());
			this.getSqlSession().update(
					"PropFeeRuleParamBauthMapper.bauthDeleteByRuleID", map);
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getEdituserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}

		/*
		 * String periodVal = propFeeCommInfoService.findValByCode(
		 * propFeeComputeRule.getCommID(), "period"); if
		 * (StringUtils.isBlank(periodVal)) { throw new AppBizException(2,
		 * "亲，请先小区配置缴费周期"); } propFeeComputeRule.setPeriodType(periodVal);
		 */
		/***
		 * 添加参数
		 */
		return this.editPropFeeComputeRule(propFeeComputeRule);

	}

	public Rule checkRule(String commID, Rule rule) throws AppBizException {

		if (!rule.isInit()) {

			rule.init(false);

		}

		if (rule.getIsValid() == -1) {

			rule.setCheckRes(false);
			rule.setErrInfo("规则：" + rule.getRuleName() + "的公式错误，请检查");
			return rule;
		}

		// 获取规则参数
		Map<String, String> params = rule.getExtparam();
		if (params == null) {

			rule.setCheckRes(true);
			return rule;
		}

		Map<String, String> checkParam = new HashMap<>();
		// 获取需验证参数

		for (String p : params.keySet()) {

			String pp = p;
			String p2 = "";
			int n = p.indexOf("__");

			if (n != -1) {
				pp = p.substring(0, n);
				p2 = p.substring(n + 2);
			}
			String rp = ruleCheckParam.get(pp);
			if (rp == null) {
				continue;
			}

			String[] lll = rp.split(",");

			for (String string : lll) {

				if (n != -1) {

					if (StringUtils.isBlank(p2)) {

						rule.setCheckRes(false);
						rule.setErrInfo("规则：" + rule.getRuleName()
								+ "的公式错误，请检查");
						return rule;
					}
					if (checkParam.get(string) == null) {
						checkParam.put(string, p2);
					} else {
						checkParam.put(string, checkParam.get(string) + ","
								+ p2);
					}

				} else {

					checkParam.put(string, null);
				}
			}

		}

		// 无验证信息
		if (checkParam.size() == 0) {

			rule.setCheckRes(true);

		}

		else {
			// 包含123的可以一起查询
			int n = 0;

			if (checkParam.containsKey("1")) {
				checkParam.remove("1");
				n++;
			}
			if (checkParam.containsKey("2")) {
				checkParam.remove("2");
				n = n + 2;
			}
			if (checkParam.containsKey("3")) {
				checkParam.remove("3");
				n = n + 4;
			}

			if (n != 0) {
				checkParam.put("0", n + "");

			}
			System.out.println(checkParam + "^^^^^^^^^^^^^^");
			for (String p : checkParam.keySet()) {

				if (!this.chekcParam(p, checkParam.get(p), rule)) {
					return rule;

				}
				;

			}

		}

		// TODO Auto-generated method stub
		return rule;
	}

	public boolean chekcParam(String key, String value, Rule rule) {

		switch (key) {
		case "0":
			return this.checkLinkAreaFloorOccupy(value, rule).getCheckRes();
		case "1":
			return this.checkLinkArea(rule).getCheckRes();
		case "2":
			return this.checkLinkOccupy(rule).getCheckRes();
		case "3":
			return this.checkLinkFloor(rule).getCheckRes();
		case "4":
			return this.checkLinkRatio(rule,value).getCheckRes();
		case "5":
			return this.checkLinkCommMeter(value, rule,"1").getCheckRes();
		case "6":
			return this.checkLinkUserMeterByType(value, rule).getCheckRes();
		case "7":
			return this.checkLinkUserFee(rule).getCheckRes();

		case "8":
			return this.checkLinkCommMeter(value, rule,"2").getCheckRes();
		default:
			break;
		}

		return false;
	}

	/**
	 * 检测关联房屋是否都有面积
	 * */

	public Rule checkLinkArea(Rule rule) {

		return rule;
	}

	/**
	 * 检测房屋是否都有入住参数
	 * */

	public Rule checkLinkOccupy(Rule rule) {

		return rule;
	}

	/**
	 * 检测房屋是否都有楼层参数
	 * */

	public Rule checkLinkFloor(Rule rule) {

		return rule;
	}

	/**
	 * 面积楼层是否入住合并验证
	 * */

	public Rule checkLinkAreaFloorOccupy(String type, Rule rule) {

		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.checkAreaOccupyFloor", rule.getRuleID());

		int n = 1;
		String e = "";
		Map<String, String> p = new HashMap<>();
		if (!"0".equals(res.get("noAreaNo").toString())) {

			p.put("1", "计费面积");

		}
		if (!"0".equals(res.get("noOccupyNo").toString())) {

			p.put("2", "入住信息");

		}

		if (!"0".equals(res.get("nofloorNo").toString())) {

			p.put("3", "楼层信息");

		}

		if ("1".equals(type)) {

			e = p.get("1") != null ? p.get("1") + "," : "";
		}
		if ("3".equals(type)) {
			e = (p.get("1") != null ? p.get("1") + "," : "")
					+ (p.get("2") != null ? p.get("2") + "," : "");

		}

		if ("7".equals(type)) {
			e = (p.get("1") != null ? p.get("1") + "," : "")
					+ (p.get("2") != null ? p.get("2") + "," : "")
					+ (p.get("3") != null ? p.get("3") + "," : "");

		}
		
		
		if (!"".equals(e)) {

			rule.setCheckRes(false);

			rule.setErrInfo("关联房屋的" + e.substring(0, e.length() - 1) + "存在空数据");
			return rule;
		}

		rule.setCheckRes(true);
		return rule;
	}

	/**
	 * 关联的产权的自行录入的费用
	 * */

	public Rule checkLinkUserFee(Rule rule) {
		rule.setCheckRes(true);
		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("ruleID", rule.getRuleID());

		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.checkLinkPollfee", param);

		if (res == null || res.get("noAmountNo") == null
				|| !"0".equals(res.get("noAmountNo").toString())) {

			rule.setCheckRes(false);
			rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
					+ rule.getRuleName() + " 公摊费用录入的费用存在空数据");
		}

		return rule;
	}

	/**
	 * 检测楼层系数参数是否配置
	 * */

	public Rule checkLinkRatio(Rule rule,String value) {

		List<Map<String, Object>> m = this.getSqlSession().selectList(
				"computeRuleMapper.getRuleLinkRatio", rule.getRuleID());

		if (m == null || m.size() > 1) {

			rule.setCheckRes(false);
			rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
					+ rule.getRuleName() + " 楼层系数配置错误");
			return rule;

		}
		Map<String, Object> mm = m.get(0);

		// @SuppressWarnings("unchecked")
		Map<String, Object> j = JSON.parseObject(
				StringUtils.toString((mm.get("paramVal"))), Map.class);
		if (j.get("ratioType") == null) {

			rule.setCheckRes(false);
			rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
					+ rule.getRuleName() + " 楼层系数配置错误");
			return rule;

		}

		else if ("1".equals(j.get("ratioType").toString())) {

			if (j.get("initNumber") == null
					|| !StringUtils.isDouble(j.get("initNumber").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;
			if (j.get("addUnitNumber") == null
					|| !StringUtils.isDouble(j.get("addUnitNumber").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;
			if (j.get("subUnitNumber") == null
					|| !StringUtils.isDouble(j.get("subUnitNumber").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;
			if (j.get("startNum") == null
					|| !StringUtils.isInt(j.get("startNum").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;
			if (j.get("addUnitClass") == null
					|| !StringUtils.isInt(j.get("addUnitClass").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;
			if (j.get("subUnitClass") == null
					|| !StringUtils.isInt(j.get("subUnitClass").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 楼层系数配置错误");
				return rule;
			}
			;

			Ratio ratio = new Ratio(j.get("ratioType").toString(), j.get(
					"initNumber").toString(), Integer.parseInt(j
					.get("startNum").toString()), j.get("addUnitNumber")
					.toString(), Integer.parseInt(j.get("addUnitClass")
					.toString()), j.get("subUnitNumber").toString(),
					Integer.parseInt(j.get("subUnitClass").toString()));
			rule.setCheckRes(true);
			rule.setRatio(ratio);
		} else 
		
	{
			
			TypeReference<List<Map<String, String>>> t = new TypeReference<List<Map<String, String>>>() {
			};
			List<Map<String, String>> rs = JSON.parseObject(j.get("ratios")
					.toString(), t);

			Ratio ratio = new Ratio(j.get("ratioType").toString(), rs);

			rule.setCheckRes(true);
			rule.setRatio(ratio);
			
			
			
		}
		
if("1".equals(j.get("ratioType").toString())||"2".equals(j.get("ratioType").toString())){
	
	List<Map<String, Object>> l = this.getSqlSession().selectList(
			"computeRuleMapper.selectFloors", rule.getRuleID());

	int n = rule.getRatio().checkSumRatio(l);

	if (n != -10000000) {

		rule.setCheckRes(false);
		rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
				+ rule.getRuleName() + " 楼层系数配置错误， 第" + n + "层无法获取系数");
		return rule;
	}

	
	
}

if("4".equals(j.get("ratioType").toString())){
	
	if(value.indexOf("@011")!=-1)
		rule=this.checkLinkUserMeterByType(value.substring(6,value.length()-1), rule);
	
	
}
		
		return rule;
	}

	/**
	 * 关联表使用量
	 * */

	public Rule checkLinkCommMeter(String meterTypes, Rule rule,String type) {
		rule.setCheckRes(true);
		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("ruleID", rule.getRuleID());

		String[] ll = meterTypes.split(",");
		for (String string : ll) {
			param.put("meterTypeID", string);
			Map<String, Object> res = this.getSqlSession().selectOne(
					"computeRuleMapper.checkLinkMeter", param);
if("1".equals(type)){
	
	if (res == null || res.get("noAmountNo") == null
			|| !"0".equals(res.get("noAmountNo").toString())) {

		rule.setCheckRes(false);
		rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + " 下规则："
				+ rule.getRuleName() + "的 关联"
				+ StringUtils.toString(res.get("meterTypeName"), "")
				+ "读数存在空数据");
		return rule;
	}	
	
}
if("2".equals(type)){
	
	
	if (res == null || res.get("noFeeAmountNo") == null
			|| !"0".equals(res.get("noFeeAmountNo").toString())) {

		rule.setCheckRes(false);
		rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + " 下规则："
				+ rule.getRuleName() + "的 关联"
				+ StringUtils.toString(res.get("meterTypeName"), "")
				+ "公摊金额存在空数据");
		return rule;
	}
		
	
}
	
		
			
			
			
		}

		return rule;
	}

	
	
	
	
	/**
	 * 关联的产权的某几个类型 表使用量
	 * */

	public Rule checkLinkUserMeterByType(String meterTypes, Rule rule) {
		rule.setCheckRes(true);
		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("ruleID", rule.getRuleID());

		String[] ll = meterTypes.split(",");
		for (String string : ll) {
			param.put("meterTypeID", string);
			Map<String, Object> res = this.getSqlSession().selectOne(
					"computeRuleMapper.checkLinkApMeter", param);

			if (res == null || res.get("noAmountNo") == null
					|| !"0".equals(res.get("noAmountNo").toString())) {

				rule.setCheckRes(false);
				rule.setErrInfo("收费类目 " + rule.getFeeTypeName() + "下规则："
						+ rule.getRuleName() + " 用户表读数存在空数据");
				return rule;
			}
		}

		return rule;
	}

	public Rule setRuleParam(Rule rule, Map<String, Object> apartmentInfo,
			Map<String, String> commparam) {

		List<String> list = new ArrayList<String>(rule.getActiveParam());
		
		//默认设置 面积
		
		
		
		//设置单价
		rule.setFeePrice(null);
		rule.setFeeAmount(null);

		
		rule.setParam(new HashMap());
		for (String param : list) {
			this.setParam(param, rule, apartmentInfo, commparam);

		}
		String feePrice=null;
		String feeAmount=null;

		if("0".equals(rule.getFeeType())){
			
			if("3".equals(rule.getRuleType())){
				//rule.setFeeAmount(StringUtils.toString(apartmentInfo.get("feeArea"), ""));
				feePrice=rule.getRule();
				
			}
			else if("1".equals(rule.getRuleType())){
				if(rule.getRule().split(";").length>=2){
					
					feePrice=rule.getRule().split(";")[2];
					
				}
				feeAmount=StringUtils.toString(apartmentInfo.get("feeArea"), "");
				
			}
			
			
		}else if("2".equals(rule.getFeeType())){
			
			if("3".equals(rule.getRuleType())){
			
				feePrice=rule.getRule();
				
			}
			else if("1".equals(rule.getRuleType())){
				
				
				if(rule.getRatio()==null){
					
					if(rule.getRule().split(";").length>=2){
						
						feePrice=rule.getRule().split(";")[2];
						
					}	
					feeAmount=rule.getFeeAmount();	
				}else{
					
//					
					if(rule.getCompRule().equals("阶梯累加计算")){
						feeAmount=rule.getFeeAmount();	
						
						
					}else{
						
						feePrice=rule.getFeePrice();
						feeAmount=rule.getFeeAmount();
					}
					
					
				}
			
				
				
			}
			
		}
		rule.setFeeAmount(feeAmount);
		rule.setFeePrice(feePrice);
		return rule;

	}

	public void setParam(String param, Rule rule,
			Map<String, Object> apartmentInfo, Map<String, String> commparam) {
		String p1 = param;
		String p2 = "";
		int n = param.indexOf("__");
		if (n != -1) {

			p1 = param.substring(0, n);
			p2 = param.substring(n + 2);
		}

		switch (p1) {

		case "@001":

			rule.setCache(p1, this.getLinkAllFeeArea(rule).get("allArea")
					.toString());

			break;
		case "@002":

//			rule.setFeeAmount(apartmentInfo.get("feeArea").toString());
			rule.getParam().put(param, apartmentInfo.get("feeArea").toString());

			break;
		case "@003":

			rule.setCache(param, this.getLinkAllFeeArea(rule).get("occupyArea")
					.toString());

			break;
		case "@004":
			rule.setCache(param,
					this.getLinkAllFeeArea(rule).get("noOccupyArea").toString());

			break;

		case "@005":
			rule.setCache(param, this.getLinkAllFeeArea(rule).get("occupy")
					.toString());

			break;
		case "@006":
			rule.setCache(param, this.getLinkAllFeeArea(rule).get("noOccupy")
					.toString());

			break;

		case "@007":
			rule.setCache(param, this.getLinkAllFeeArea(rule).get("total")
					.toString());

			break;
		case "@008":
			rule.getParam().put(
					param,
					this.getLinkRatio(apartmentInfo.get("floor").toString(),
							rule));
			break;

		case "@009":
			rule.setCache(param, this.getLinkSumRatio(rule));

			break;
		case "@010":

			rule.setCache(param, this.getLinkCommMeterAmount(p2, rule,"1"));

			break;
			
		case "@011":
			String a = this.getLinkApMeterAmount(p2,
					apartmentInfo.get("apartmentID").toString(), rule);
//			rule.setFeeAmount(a);
			
			if(rule.getFeeType().equals("2")){
				
				rule.setFeeAmount(a);
			}
			rule.getParam().put(param, a);

			break;
		case "@012":
			rule.setCache(param, this.getLinkApMeterSumAmount(p2, rule));

			break;
		case "@013":

			rule.getParam().put(
					param,
					this.getLinkPollfeeByAp(apartmentInfo.get("apartmentID")
							.toString(), rule));
			break;
		case "@014":

			p2=p2.substring(0,p2.length()-1);
			if(rule.getParam().get(p2)==null){
				
				
				this.setParam(p2, rule, apartmentInfo, commparam);
			}
			
			String pp=this.getLinkRatioPrice(rule.getParam().get(p2), rule);
			rule.getParam().put(
					param,
					pp);
			
              if(rule.getFeeType().equals("2")){
				
				rule.setFeePrice(pp);
			}
			
			

			break;
			
			
		case "@015":

			rule.setCache(param, this.getLinkCommMeterAmount(p2, rule,"2"));

			break;
		default:
			break;
		}

	}

	/**
	 * 获取关联用户总面积
	 * */

	private Map<String, Object> getLinkAllFeeArea(Rule rule) {

		Map<String, Object> map = this.getSqlSession().selectOne(
				"computeRuleMapper.getLinkApartInfo", rule.getRuleID());
		return map;

	}

	/**
	 * 获取楼层系数
	 * */
	private String getLinkRatio(String floor, Rule rule) {
		if (rule.getRatio() == null) {

			this.checkLinkRatio(rule,null);
		}

		if (rule.getRatio() == null) {

			return null;
		}
		return rule.getRatio().getFloorRatio(Integer.parseInt(floor));
	}
	/**
	 * 获取计量单价
	 * */
	private String getLinkRatioPrice(String floor, Rule rule) {
		
		
		if(floor==null){
			
			
			return null;
		}
		if (rule.getRatio() == null) {

			this.checkLinkRatio(rule,null);
		}

		if (rule.getRatio() == null) {

			return null;
		}
		
		
		
		return rule.getRatio().getRario(floor);
	}

	/**
	 * 获取楼层系数总和
	 * */
	private String getLinkSumRatio(Rule rule) {

		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"computeRuleMapper.selectFloors", rule.getRuleID());

		return rule.getRatio().getSumRatio(l);
	}

	/**
	 * 获取关联用户入住总面积
	 * */

	private String getLinkOccupyFeeArea(Rule rule) {

		return "";

	}

	/**
	 * 获取关联用户未入住面积
	 * */

	private String getLinkNoOccupyFeeArea(Rule rule) {

		return "";

	}

	/**
	 * 获取关联相同类型的关联表读数总和
	 * */
	private String getLinkCommMeterAmount(String type, Rule rule,String type1) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("ruleID", rule.getRuleID());
		param.put("meterTypeID", type);
		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.getLinkMeterSumBytype", param);

		if("1".equals(type1)){
			
			if (res == null || res.get("useAmount") == null
					|| !StringUtils.isDouble(res.get("useAmount").toString())) {

				return null;
			}

			return res.get("useAmount").toString();
			
		}
		

		if("2".equals(type1)){
			
			if (res == null || res.get("feeAmount") == null
					|| !StringUtils.isDouble(res.get("feeAmount").toString())) {

				return null;
			}

			return res.get("feeAmount").toString();
			
		}
return null;

	}

	/**
	 * 获取某类型下的业主表读数
	 * */
	private String getLinkApMeterAmount(String type, String apartmentID,
			Rule rule) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("apartmentID", apartmentID);
		param.put("meterTypeID", type);
		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.getLinkApMeterAmountBytype", param);

		if (res == null || res.get("useAmount") == null
				|| !StringUtils.isDouble(res.get("useAmount").toString())) {

			return null;
		}

		return res.get("useAmount").toString();

	}

	/**
	 * 获取某类型下的业主表读数总和
	 * */
	private String getLinkApMeterSumAmount(String type, Rule rule) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());

		param.put("ruleID", rule.getRuleID());
		param.put("meterTypeID", type);
		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.getLinkApMeterSumAmountBytype", param);

		if (res == null || res.get("useAmount") == null
				|| !StringUtils.isDouble(res.get("useAmount").toString())) {

			return null;
		}

		return res.get("useAmount").toString();

	}

	/**
	 * 获取房屋自行缴费
	 * */
	private String getLinkPollfeeByAp(String apartmentID, Rule rule) {

		Map<String, String> param = new HashMap<String, String>();
		param.put("endDate", rule.getEndDate());
		param.put("apartmentID", apartmentID);
		param.put("ruleID", rule.getRuleID());
		param.put("linkID",rule.getInitID());
		Map<String, Object> res = this.getSqlSession().selectOne(
				"computeRuleMapper.getLinkPollfeeByAp", param);

		if (res == null || res.get("useAmount") == null
				|| !StringUtils.isDouble(res.get("useAmount").toString())) {

			return null;
		}

		return res.get("useAmount").toString();

	}

	@Override
	public List<Map<String, Object>> getApartmentsByRuleID(String ruleID)
			throws AppBizException {

		List<Map<String, Object>> l = this.getSqlSession().selectList(
				"computeRuleMapper.getLinkApartments", ruleID);

		return l;
	}

	@Override
	public Map<String, Object> getOtherMoney(
			Map<String, Object> propFeeComputeRule) throws AppBizException {

		if (propFeeComputeRule == null || propFeeComputeRule.size() <= 0) {
			throw new AppBizException(2, "亲，请填写相关信息");
		}

		if (!propFeeComputeRule.containsKey("commID")) {
			throw new AppBizException(2, "亲，请填写小区信息");
		}
		if (propFeeComputeRule.get("commID") == null) {
			throw new AppBizException(2, "亲，请填写小区信息");
		}

		if (!propFeeComputeRule.containsKey("feeTypeID")) {
			throw new AppBizException(2, "亲，请填写费用类目信息");
		}
		if (propFeeComputeRule.get("feeTypeID") == null) {
			throw new AppBizException(2, "亲，请填写费用类目信息");
		}

		if (!propFeeComputeRule.containsKey("apartmentID")) {
			throw new AppBizException(2, "亲，请填写房屋信息");
		}
		if (propFeeComputeRule.get("apartmentID") == null) {
			throw new AppBizException(2, "亲，请填写房屋信息");
		}
//		logger.info("获取别名："+propFeeComputeRule.get("otherName"));
//		if (propFeeComputeRule.get("otherName").equals("undefined") || propFeeComputeRule.get("otherName")=="" ||
//				propFeeComputeRule.get("otherName")==null) {
//			propFeeComputeRule.remove("otherName");
//		}

		propFeeComputeRule.put("belongType", "2");
		List<Map<String, Object>> ruleResult = this.getSqlSession().selectList(
				"PropFeeComputeRuleSearchMapper.getRuleByApartMentID",
				propFeeComputeRule);
		if (ruleResult == null || ruleResult.size() <= 0) {
			throw new AppBizException(2, "亲，该房屋不存在相关的缴费信息");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> houseinfo = new HashMap<String, Object>();
		int n=ruleResult.size();
		
		if(n==1){
			Map<String, Object> map=ruleResult.get(0);
			if("2".equals(map.get("rule_type").toString())){
				
				
				param.put("ruleType", "2");
				param.put("ruleID", map.get("rule_id"));
				Map<String, Object> houseResult = this.getSqlSession().selectOne(
						"PropFeeComputeRuleSearchMapper.getHouseInfoByApartMentID",
						propFeeComputeRule);
				houseinfo.put("ownerName", houseResult.get("ownerName"));
				houseinfo.put("buildingName", houseResult.get("building_name"));
				houseinfo.put("unitName", houseResult.get("unit_name"));
				houseinfo.put("apartmentNum", houseResult.get("apartment_num"));
				houseinfo.put("feeArea", houseResult.get("fee_area"));
				param.put("feeTypeName", map.get("fee_type_name"));
				param.put("feeTypeID", map.get("fee_type_id"));
				param.put("houseInfo", houseinfo);
				param.put("propfee", "");
				return  param;
			}
			
			
		}else{
			
			for (Map<String, Object> map : ruleResult) {
				
				if("2".equals(map.get("rule_type").toString())){
					throw new AppBizException(2, "计算规则配置异常");	
					
					
				}
				
			//	rule.get("ruleType")
			}
			
		}
	
		BigDecimal propfee = new BigDecimal(0);
		if (ruleResult != null && ruleResult.size() > 0) {
			for (Map<String, Object> rule : ruleResult) {
				rule = PropertyUtils.turnToEntity1(rule);
				/***
				 * 判断是否已经缴费
				 */
				/*
				 * List<PropFeePaybillDto> propFeePaybills = payBillService
				 * .findPropFeePaybillByRelevantID(null,
				 * propFeeComputeRule.get("commID").toString(),
				 * propFeeComputeRule.get("apartmentID").toString(), null, null,
				 * "3", null, null, "2", null, "1",
				 * ruleResult.get("ruleID").toString()); if (propFeePaybills !=
				 * null && propFeePaybills.size() > 0) { throw new
				 * AppBizException(2, "亲，该房屋不能存在相关的缴费信息"); }
				 */

				List<PropFeeDetailDto> detailsDetail = feeDetailService
						.findPropFeeDetailByRelevantID(null, propFeeComputeRule
								.get("apartmentID").toString(),
								propFeeComputeRule.get("feeTypeID").toString(),
								null, null, null, "1", rule.get("ruleID")
										.toString(), null);


				if ("2".equals(rule.get("validityType").toString())) {
					
					if (detailsDetail != null && detailsDetail.size() > 0) {

						for (PropFeeDetailDto propFeeDetailDto : detailsDetail) {
							if (!"1".equals(propFeeDetailDto.getStatus())) {
								propfee = propfee.add(propFeeDetailDto.getAmount());

							}
						}
						
						
						

					}
					
					
					
					

				}else{
					
					
					
					String money = this.completeRule(rule.get("ruleID")
							.toString(), propFeeComputeRule.get("commID")
							.toString(), propFeeComputeRule.get("apartmentID")
							.toString());
					
					
					if (!StringUtils.isBlank(money)) {
						// throw new AppBizException(2, "亲，该房屋不能存在相关的缴费信息");
						propfee = propfee.add(new BigDecimal(money));
					}

					
					
					
					
					
				}
//				
//				
//				if (detailsDetail != null && detailsDetail.size() > 0) {
//
//					for (PropFeeDetailDto propFeeDetailDto : detailsDetail) {
//						if (!"1".equals(propFeeDetailDto.getStatus())) {
//							propfee = propfee.add(propFeeDetailDto.getAmount());
//
//						}
//					}
//					
//					
//					
//
//				} else {
//
//					if ("2".equals(rule.get("validityType").toString())) {
//
//						continue;
//
//					}
//
//				
//				}

			}
		}

		if (propfee.compareTo(new BigDecimal("0")) <= 0) {
			throw new AppBizException(2, "亲，该房屋不存在相关的缴费信息");
		}
		Map<String, Object> houseResult = this.getSqlSession().selectOne(
				"PropFeeComputeRuleSearchMapper.getHouseInfoByApartMentID",
				propFeeComputeRule);

		houseinfo.put("ownerName", houseResult.get("ownerName"));
		houseinfo.put("buildingName", houseResult.get("building_name"));
		houseinfo.put("unitName", houseResult.get("unit_name"));
		houseinfo.put("apartmentNum", houseResult.get("apartment_num"));
		houseinfo.put("feeArea", houseResult.get("fee_area"));
		
		param.put("feeTypeName", ruleResult.get(0).get("fee_type_name"));
		param.put("feeTypeID", ruleResult.get(0).get("fee_type_id"));
		
		param.put("houseInfo", houseinfo);
//		param.put("houseInfo", PropertyUtils.turnToEntity1(houseResult));
		
		
	//param.put("rule", PropertyUtils.turnToEntity(ruleResult));
		param.put("propfee", propfee);
		param.put("ruleType", "0");
		param.put("ruleID", "0");
	//	param.put("ruleType", propfee);

		return param;
	}

	public String completeRule(String ruleID, String commID, String apartmentID)
			throws AppBizException {
		// 获取规则信息

		Rule rule = this.getRule(ruleID);
		if (rule == null) {

			return null;
		}
		// 获取房屋信息

		Map<String, Object> apart = propInfoService
				.getApartmentInfo(apartmentID);

		apart = PropertyUtils.turnToEntity1(apart);
		if (apart == null) {

			return null;
		}

		rule.init(false);
		if (rule.getIsValid() == -1) {

			return null;
		}

		if (!this.checkRule(commID, rule).getCheckRes()) {
			return null;
			// continue;
		}

		this.setRuleParam(rule, apart, new HashMap());
		BigDecimal res = rule.complete(rule.getParam());

		if (res == null) {

			return null;
		}

		res = res.setScale(rule.getRound(), BigDecimal.ROUND_HALF_UP);
		if (res.compareTo(BigDecimal.ZERO) > 0) {

			return res.toString();
		}

		// TODO Auto-generated method stub
		return null;

	};

	// 获取单个规则
	private Rule getRule(String ruleID) {

		Rule r = this.getSqlSession().selectOne(
				"computeRuleMapper.selectByPrimaryKey", ruleID);
		return r;
	}

	// 获取单个规则
	private List<Rule> getRules(String commID, String apartmentID,
			String[] feeTypeIDs, String startTime, String endTime) {

		Map<String, Object> param = new HashMap<>();
		param.put("commID", commID);
		param.put("apartmentID", apartmentID);

		param.put("feeTypeIDs", feeTypeIDs);
		param.put("startTime", startTime);
		param.put("endTime", endTime);

		List<Rule> rules = this.getSqlSession().selectList(
				"computeRuleMapper.selectRuleByFeeTypeIDs", param);

		return rules;
	}

	@Override
	public List<Map<String, Object>> completeRule(String commID,
			String apartmentID, String[] feeTypeIDs, String startTime,
			String endTime) throws AppBizException {

		List<Rule> rules = this.getRules(commID, apartmentID, feeTypeIDs,
				startTime, endTime);

		if (rules == null || rules.size() == 0) {

			return null;

		}
		List<Map<String, Object>> res = new ArrayList<Map<String, Object>>();
		// 获取房屋信息

		Map<String, Object> apart = propInfoService
				.getApartmentInfo(apartmentID);

		for (Rule rule : rules) {

			rule.init();
			if (rule.getIsValid() == -1) {

				continue;
			}
			this.setRuleParam(rule, apart, null);

			Map<String, Object> r = new HashMap<String, Object>();

			r.put("feeTypeID", rule.getFeeTypeID());
			String res1 = rule.completeString(rule.getParam());
			if (StringUtils.isBlank(res1)) {

				continue;
			}
			r.put("amount", res1);
			res.add(r);

		}

		//

		// TODO Auto-generated method stub
		return res;
	}

	@Override
	public List<Map<String, Object>> getFeeComputeRuleType(
			Map<String, Object> propFeeComputeRule) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeComputeRuleSearchMapper.getFeeComputeRuleType",
				propFeeComputeRule);
		return list;
	}

	@Override
	public int deletePropFeeComputeRule(PropFeeComputeRuleDto propFeeComputeRule)
			throws AppBizException {

		/***
		 * 删除关联连接
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", propFeeComputeRule.getRuleID());
		map.put("invalid", "0");
		map.put("edituserID", propFeeComputeRule.getEdituserID());
		this.getSqlSession()
				.update("PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleIDs",
						map);
		this.getSqlSession().update(
				"PropFeeRuleLinkMeterBauthMapper.bauthDeleteByRuleIDs", map);
		this.getSqlSession().update(
				"PropFeeRuleParamBauthMapper.bauthDeleteByRuleIDs", map);

		propFeeComputeRule.setItemStat(TypeConst.FLAG_OFF);
		propFeeComputeRule.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeComputeRuleMapper.updateByPrimaryKeySelective",
				propFeeComputeRule);
	}
}
